package com.wenguo.action.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenguo.Constants;
import com.wenguo.bussines.model.Group;
import com.wenguo.bussines.model.ResultMessage;
import com.wenguo.bussines.model.User;
import com.wenguo.common.util.BeansUtil;
import com.wenguo.common.util.MD5;
import com.wenguo.common.util.TimeUtil;
import com.wenguo.common.util.URLHelper;
import com.wenguo.common.util.URLHelper.PageInfo;
import com.wenguo.exception.WenGuoException;


@Controller
@RequestMapping("/admin/*")
public class AdminAction extends BaseAction {

	@RequestMapping("user/list/{page}")
	public String userList(@PathVariable String page, HttpServletRequest request, Model model){
		try {
			PageInfo pageInfo = URLHelper.getPageInfo(request);
			pageInfo.setTotalCount(facade.findCountOfUser(null));
			List<User> ulist = facade.findUserByPage(BeansUtil.getUserQueryParams(pageInfo, request));
			
			model.addAttribute(Constants.system.PAGE_BEAN, pageInfo);
			model.addAttribute(Constants.system.TAG_LIST, ulist);
			return "admin/user/list";
		} catch (WenGuoException e) {
			logger.error(e);
		}
		model.addAttribute(Constants.system.TIPS, "get user list exception");
		return "admin/user/list";
	}
	@RequestMapping("user/toAdd")
	public String toUserAdd(Model model){
		try {
			List<Group> groupList = facade.findAllGroup();
			model.addAttribute(Constants.system.TAG_LIST, groupList);
			model.addAttribute(Constants.system.TEMP, TimeUtil.getShortFormatLaterNyearDate(3));
			return "admin/user/add";
		} catch (WenGuoException e) {
			logger.error(e);
		}
		model.addAttribute(Constants.system.TIPS, "get group list exception");
		return "admin/user/add";
	}
	
	@RequestMapping(value = "user/add", method = RequestMethod.POST)
	public @ResponseBody ResultMessage userAdd(User user){
		ResultMessage rm = new ResultMessage();
		try {
			user.setPass(MD5.code(user.getPass()));
			String date = TimeUtil.getFullFormatDate();
			user.setLast(date);
			user.setRegist(date);
			
			if (!facade.createUser(user)) {
				rm.setReturnCode(Constants.status.FAILED);
			}
			
		} catch (WenGuoException e) {
			logger.error(e);
		}
		
		return rm;
	}
	
	@RequestMapping("user/toUpdate")
	public String toUserUpdate(@RequestParam String id, Model model){
		try {
			List<Group> groupList = facade.findAllGroup();
			model.addAttribute(Constants.system.TAG_LIST, groupList);
			User user = facade.findUserById(id);
			model.addAttribute(Constants.system.TAG_BEAN, user);
			return "admin/user/update";
		} catch (WenGuoException e) {
			logger.error(e);
		}
		model.addAttribute(Constants.system.TIPS, "get group list/user info exception");
		return "admin/user/update";
	}
	
	@RequestMapping(value = "user/update", method = RequestMethod.POST)
	public @ResponseBody ResultMessage userUpdate(User user){
		ResultMessage rm = new ResultMessage();
		try {
			if (!facade.updateUser(user)) {
				rm.setReturnCode(Constants.status.FAILED);
			}
		} catch (WenGuoException e) {
			logger.error(e);
		}
		
		return rm;
	}
	
	@RequestMapping("user/toUpdatePwd")
	public String toUserUpdatePwd(@RequestParam String id, Model model){
		
		model.addAttribute(Constants.system.ID, id);
		return "admin/user/updatePwd";
	}
	
	@RequestMapping(value = "user/updatePwd", method = RequestMethod.POST)
	public @ResponseBody ResultMessage userUpdatePwd(User user){
		ResultMessage rm = new ResultMessage();
		try {
			user.setPass(MD5.code(user.getPass()));
			if (!facade.updateUserPassword(user)) {
				rm.setReturnCode(Constants.status.FAILED);
			}
		} catch (WenGuoException e) {
			logger.error(e);
		}
		
		return rm;
	}
	
	@RequestMapping(value = "user/delete", method = RequestMethod.POST)
	public @ResponseBody ResultMessage userDelete(@RequestParam String id){
		ResultMessage rm = new ResultMessage();
		try {
			if (!facade.deleteUserById(id)) {
				rm.setReturnCode(Constants.status.FAILED);
			}
		} catch (WenGuoException e) {
			logger.error(e);
		}
		
		return rm;
	}
	
	@RequestMapping("user/search")
	public String userSearch(@RequestParam String id, HttpServletRequest request, Model model){
		try {
			PageInfo pageInfo = URLHelper.getPageInfo(request);
			User user = new User();
			user.setId(id);
			pageInfo.setTotalCount(facade.findCountOfUser(user));
			List<User> ulist = facade.findUserByPage(BeansUtil.getUserQueryParams(pageInfo, request));
			
			model.addAttribute(Constants.system.PAGE_BEAN, pageInfo);
			model.addAttribute(Constants.system.TAG_LIST, ulist);
			return "admin/user/list";
		} catch (WenGuoException e) {
			logger.error(e);
		}
		model.addAttribute(Constants.system.TIPS, "get search user list exception");
		return "admin/user/list";
	}
	
}
